package com.bigevent.service.impl;

import com.bigevent.mapper.ManagerMapper;
import com.bigevent.pojo.PageBean;
import com.bigevent.pojo.User;
import com.bigevent.service.ManagerService;
import com.bigevent.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public PageBean<User> listAuthor(Integer pageNum,Integer pageSize) {

        // 1.创建pageBean对象
        PageBean<User> pb = new PageBean<>();

        PageHelper.startPage(pageNum,pageSize);

        List<User> list = managerMapper.listAuthor();

        Page<User> p = (Page<User>) list;

        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void updateStatus(Integer userId) {
        managerMapper.updateStatus(userId);
    }

    @Override
    public void cancelStatus(Integer userId) {

        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        if (id.equals(userId)){
            throw new RuntimeException("不能修改自己的职位");
        }else if (userId == 9){
            throw new RuntimeException("不能修改超级管理员的职位");
        }
        managerMapper.cancelStatus(userId);
    }

    @Override
    public void delete(Integer userId) {
        if (userId == 9){
            throw new RuntimeException("不能删除超级管理员");
        }
        managerMapper.delete(userId);
    }
}
